<template>
  <div class="user-box">
    <div class="setting-box">
      <el-button size="mini" @click="dialogVisible = true">新增</el-button>
      <el-form :inline="true" :model="userForm" class="demo-form-inline">
        <el-form-item >
          <el-input
            v-model="userForm.name"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-dialog
      title="新增"
      :visible.sync="dialogVisible"
      width="35%"
      :before-close="handleClose"
    >
      <!-- 新增用户表单 -->
      <el-form
        :rules="rules"
        :inline="true"
        ref="form"
        :model="form"
        label-width="80px"
      >
        <el-form-item prop="name" label="姓名">
          <el-input placeholder="请输入姓名" v-model="form.name"></el-input>
        </el-form-item>

        <el-form-item prop="age" label="年龄">
          <el-input placeholder="请输入年龄" v-model="form.age"></el-input>
        </el-form-item>

        <el-form-item prop="sex" label="性别">
          <el-select v-model="form.sex" placeholder="请选择">
            <el-option label="男" :value="1"></el-option>
            <el-option label="女" :value="0"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item prop="birth" label="出生日期">
          <el-date-picker
            type="date"
            placeholder="选择日期"
            v-model="form.birth"
            value-format="yyyy-MM-dd"
          ></el-date-picker>
        </el-form-item>

        <el-form-item prop="addr" label="地址">
          <el-input placeholder="请输入地址" v-model="form.addr"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="cance()">取 消</el-button>
        <el-button type="primary" @click="submit()">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 用户表格 -->
    <el-table :data="tableData" stripe style="width: 100%">
      <el-table-column prop="name" label="姓名" width="180"> </el-table-column>
      <el-table-column prop="sex" label="性别" width="180">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{
            scope.row.sex === 1 ? "男" : "女"
          }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="age" label="年龄" width="180"> </el-table-column>
      <el-table-column prop="birth" label="出生日期" width="180">
      </el-table-column>
      <el-table-column prop="addr" label="地址" width="180"> </el-table-column>
      <!-- 操作 -->
      <el-table-column label="操作" width="180">
        <template slot-scope="scope">
          <span style="margin-left: 10px">
            <el-button size="mini" @click="handelEdit(scope.row)"
              >编辑</el-button
            >
            <el-button size="mini" type="danger" @click="handelDelt(scope.row)"
              >删除</el-button
            >
          </span>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <el-pagination
      @current-change="changePage"
      layout="prev, pager, next"
      :total="total"
    >
    </el-pagination>
  </div>
</template>

<style scoped>
</style>

<script>
import { getuser, addUser, editUser, delUser } from "../api/index.js";

export default {
  data() {
    return {
      dialogVisible: false,
      form: {
        name: "",
        age: "",
        sex: "",
        birth: "",
        addr: "",
      },
      rules: {
        name: [{ required: true, message: "请输入姓名", trigger: "blur" }],
        age: [{ required: true, message: "请输入年龄", trigger: "blur" }],
        sex: [{ required: true, message: "请输入性别", trigger: "blur" }],
        birth: [{ required: true, message: "请输入出生日期", trigger: "blur" }],
        addr: [{ required: true, message: "请输入地址", trigger: "blur" }],
      },
      tableData: [],
      // 弹窗的状态
      modelType: 0,
      total: 0,
      // 当前的页码
      pageData: {
        page: 1,
        limit: 10,
      },
      userForm:{
        name:''
      }
    };
  },
  methods: {
    // 提交用户表单
    submit() {
      this.$refs.form.validate((val) => {
        if (val) {
          console.log(this.form);
          if (this.modelType === 0) {
            // modeType===0时是新增用户
            addUser(this.form).then(() => {
              // 重新获取用户数据
              this.getList();
            });
          } else {
            // modeType !==0时是编辑用户
            editUser(this.form).then(() => {
              // 重新获取用户数据
              this.getList();
            });
          }

          // 关闭弹窗
          this.dialogVisible = false;
          // 初始化表单
          this.$refs.form.resetFields();
        }
      });
    },
    // 查询
    onSubmit(){
      this.getList()
    },
    // 获取用户列表数据
    getList() {
      getuser({ params:{...this.userForm,...this.pageData}  }).then(({ data }) => {
        console.log(data);
        this.tableData = data.list;
        this.total = data.count;
      });
    },

    // 编辑
    handelEdit(row) {
      // console.log(row);
      this.modelType = 1;
      this.dialogVisible = true;
      //传的参数不能是原数据，因为要更改原数据，做一个深拷贝
      this.form = JSON.parse(JSON.stringify(row));
    },
    // 删除
    handelDelt(row) {
      this.$confirm("此操作将永久删除该用户, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delUser({ id: row.id }).then(() => {
            console.log("000");
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            // 重新获取用户数据
            this.getList();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 分页器切换页码
    changePage(val) {
      this.pageData.page = val;

      // 重新获取用户数据
      this.getList();
    },

    handleClose() {
      // 关闭弹窗
      this.dialogVisible = false;
      // 初始化表单
      this.$refs.form.resetFields();
    },
    // 取消
    cance() {
      this.handleClose();
    },
  },
  mounted() {
    this.getList();
  },
};
</script>